package com.dcy.znoa.service.publiz.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.publiz.model.canteen.SelectCanteenModel;
import com.dcy.znoa.dao.publiz.CanteenDao;
import com.dcy.znoa.dao.publiz.PlaceDao;
import com.dcy.znoa.domain.publiz.Canteen;
import com.dcy.znoa.domain.publiz.Place;
import com.dcy.znoa.service.publiz.CanteenService;
import com.dcy.znoa.service.publiz.PlaceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CanteenServiceImpl implements CanteenService {

    @Autowired
    CanteenDao canteenDao;
    @Autowired
    PlaceService placeService;
    @Autowired
    PlaceDao placeDao;

    @Transactional
    @Override
    public int insert(Canteen var1) throws BusinessException {
        int result1 = canteenDao.insert(var1);
        if(result1<=0){
            return result1;
        }
        //增加对应的场所
        int placeLevel = 0;//场所级别：从1开始，1为最大(园区级)，2级是1级的下级部门（公司级）
        int entityType = 1;////所属类型 1 园区 2 企业 如果不传，就默认1
        int placetype = 3;// 场所归属类型：1 园区 2 公司  3 食堂 4 停车场 99 其他
        Long pPlaceId = 1L;//默认是园区
        if(var1.getEntityType()!=null){//
            entityType = var1.getEntityType();
        }
        if(entityType ==1){
            placeLevel = 2;
        }else if(entityType == 2){
            placeLevel = 3;
            //查询对应的上级：即公司的placeId
            Place queryPlace =new Place();
            queryPlace.setEntityId(var1.getEntityId());
            queryPlace.setPPlaceId(1L);
            queryPlace.setPlaceType(2);
            Place compPlace = placeDao.selectOne(queryPlace);
            pPlaceId = compPlace.getPlaceId();
        }else {
            throw new BusinessException("404","entityType 只能为1或2");
        }
        Place place = new Place( var1.getCanteenName(),  placeLevel,  placetype, pPlaceId, var1.getCanteenId(),var1.getCreateOperId());
        int result2 = placeService.insert(place);
        if(result2 <=0){
            return result2;
        }
        return 1;
    }

    @Override
    public int updateById(Canteen var1) {
        return canteenDao.updateById(var1);
    }

    @Override
    public int deleteById(Long id) throws BusinessException {
        return canteenDao.deleteById(id);
    }

    @Override
    public JsonForList selectList(SelectCanteenModel model) {
        Page page = new Page(model.getPageNum(), model.getPageSize());
        EntityWrapper ew = new EntityWrapper();
        Canteen canteen = new Canteen();
        ew.setEntity(canteen);
        if (StringUtils.isNotBlank(model.getCanteenName())) {
            ew.like("canteen_name", model.getCanteenName());
        }
        if (model.getEntityType() != null) {
            ew.eq("entity_type", model.getEntityType());
            if (model.getEntityId() != null) {
                ew.eq("entity_id", model.getEntityId());
            }
        }
        if (model.getValidFlag() != null) {
            ew.eq("valid_flag", model.getValidFlag());
        }
        List<Canteen> list = canteenDao.selectMapsPage(page, ew);
//        for (News news:list) {
//            news.setStoreFileList(storeFileService.selectStoreFileByEntityId(news.getNewsId(),"news",null));
//        }
        return new JsonForList(list, page.getTotal());
    }
}
